package cn.labbook.bluetooth.controller;

import cn.labbook.bluetooth.common.ResultDto;
import cn.labbook.bluetooth.common.SmsTemplateEnum;
import cn.labbook.bluetooth.entity.Ammeter;
import cn.labbook.bluetooth.entity.User;
import cn.labbook.bluetooth.entity.WarnAmmeter;
import cn.labbook.bluetooth.service.IAmmeterService;
import cn.labbook.bluetooth.service.IUserService;
import cn.labbook.bluetooth.service.IWarnAmmeterService;
import cn.labbook.bluetooth.util.GenerateIdUtils;
import cn.labbook.bluetooth.util.MessageUtil;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
@RequestMapping({"/feedback"})
public class FeedbackController extends AbstractController {

    @Autowired
    private IAmmeterService ammeterService;
    @Autowired
    private IWarnAmmeterService warnAmmeterService;
    @Autowired
    private IUserService userService;


    @RequestMapping({"/syncFail"})
    @ResponseBody
    public ResultDto syncFail(String ammeterId) {
        WarnAmmeter warnAmmeter = new WarnAmmeter();
        warnAmmeter.setAmmeterId(ammeterId);
        warnAmmeter.setGmtCreate(new Date());
        warnAmmeter.setId(GenerateIdUtils.get32Varchar());
        warnAmmeter.setLocation((this.ammeterService.getById(ammeterId)).getLocation());
        warnAmmeter.setWarnType("充值后电表同步失败");
        this.warnAmmeterService.save(warnAmmeter);
        Ammeter ammeter = this.ammeterService.getById(ammeterId);
        User user = this.userService.getById(ammeter.getUserId());
        MessageUtil.sendFailMessage(user.getMobilephone(), "平台数据与电表同步时出错,请重新扫码同步电表数据", SmsTemplateEnum.REFUND_APPLICATION.getCode());
        return success();
    }
}
